<?php

/**
 * product actions.
 *
 * @package    sf_sandbox
 * @subpackage product
 * @author     Hindua88
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class productActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->aim_products = Doctrine_Core::getTable('AimProduct')
      ->createQuery('a')
      ->limit(sfConfig::get('app_size_of_page'))
      ->execute();
  }
  
  public function isValidEmail($email){
    return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email);
  }  
  
  public function executeVote(sfWebRequest $request) {
    $fullname = $request->getParameter('fullname');
    $email = $request->getParameter('email');
    $content = $request->getParameter('content');
    $pid = intval($request->getParameter('pid'));
    if ($fullname && $email && $content  && $pid && $this->isValidEmail($email)) {
      $document = new AimProductComment();
      $document->name = $fullname;
      $document->email = $email;
      $document->content = $content;
      $document->product_id = $pid;
      $document->save();
      $this->redirect('product/show?id='.$pid);
    }
    $this->redirect('product/show?id='.$pid);
  }
  
  public function executeShow(sfWebRequest $request)
  {
    $this->aim_products = Doctrine_Core::getTable('AimProduct')
      ->createQuery('a')
      ->limit(sfConfig::get('app_sn_product_new'))
      ->execute();
    $this->product = Doctrine_Core::getTable('AimProduct')->find(array($request->getParameter('id')));
  }
  
  public function executeNew(sfWebRequest $request)
  {
    $this->form = new AimProductForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new AimProductForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($aim_product = Doctrine_Core::getTable('AimProduct')->find(array($request->getParameter('id'))), sprintf('Object aim_product does not exist (%s).', $request->getParameter('id')));
    $this->form = new AimProductForm($aim_product);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($aim_product = Doctrine_Core::getTable('AimProduct')->find(array($request->getParameter('id'))), sprintf('Object aim_product does not exist (%s).', $request->getParameter('id')));
    $this->form = new AimProductForm($aim_product);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($aim_product = Doctrine_Core::getTable('AimProduct')->find(array($request->getParameter('id'))), sprintf('Object aim_product does not exist (%s).', $request->getParameter('id')));
    $aim_product->delete();

    $this->redirect('product/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $aim_product = $form->save();

      $this->redirect('product/edit?id='.$aim_product->getId());
    }
  }
}
